
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            audio {
                width: 200px;
                height: 40px;
            }
        </style>
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户编号</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block">
                                <input type="text" autocomplete="off" name="user_sn" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <!--<div class="layui-form-item">-->
                        <!--<label class="layui-form-label">小说</label>-->
                        <!--<div class="layui-input-block">-->
                            <!--<div name="novel_sn" id="novel_sn" value="" ></div>-->
                        <!--</div>-->
                    <!--</div>-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">创作内容</label>
                        <div class="layui-input-block">
                            <div name="chapterid" id="chapterid" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">推文类型</label>
                        <div class="layui-input-block">
                            <div name="plan_type" id="plan_type" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">优秀案例</label>
                        <div class="layui-input-block">
                            <div name="is_example" id="is_example" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <div name="status" id="status" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">删除</label>
                        <div class="layui-input-block">
                            <div name="isdel" id="isdel" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">添加时间</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block" id="created_at">
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-start" class="layui-input inline-block" placeholder="开始时间">
                                -
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-end" class="layui-input inline-block" placeholder="结束时间">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <!--<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="app.admin.user.works.insert">-->
                <!--<i class="layui-icon layui-icon-add-1"></i>新增-->
            <!--</button>-->
            <!--<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="app.admin.user.works.delete">-->
                <!--<i class="layui-icon layui-icon-delete"></i>删除-->
            <!--</button>-->
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            {{#  if(d.is_example == 1 ){ }}
              <button class="pear-btn pear-btn-xs tool-btn " lay-event="cancel_example" permission="app.admin.user.works.example" style="color: #ff5722;">取消案例</button>
            {{#  } else { }}
              <button class="pear-btn pear-btn-xs tool-btn" lay-event="example" permission="app.admin.user.works.example" style="color: #1e9fff">设为案例</button>
            {{#  } }}

            <button class="pear-btn pear-btn-xs tool-btn" lay-event="part" permission="app.admin.user.parts.select">视频分段</button>
            <!--<button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.user.works.update">编辑</button>-->
            <!--<button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.user.works.delete">删除</button>-->
        </script>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>

            let user_sn = layui.url().search['user_sn']??'';
            let novel_sn = layui.url().search['novel_sn']??'';
            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/user/works/select" + location.search;
            const PART_URL = "/app/admin/user/parts/index";
            const VIEW_URL = "/app/admin/novel/chapter/view";
            const ARTICLE_VIEW_URL = "/app/admin/user/article/index";
            const VIDEO_VIEW_URL = "/app/admin/user/video/index";
            const EXAMPLE_URL = "/app/admin/user/works/example";

            let where = {};
            if(user_sn) where['user_sn'] = user_sn;
            if(novel_sn) where['novel_sn'] = novel_sn;


            layui.$('input[name="user_sn"]').val(user_sn);

            // 字段 添加时间 created_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#created_at",
                    range: ["#created_at-date-start", "#created_at-date-end"],
                    type: "datetime",
                    mark: date_today_mark,
                });
            })

            // 字段 状态 status
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#status").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#status",
                    name: "status",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"0","name":"待入列"},{"value":"1","name":"生成中"},{"value":"2","name":"已完成"},{"value":"3","name":"失败"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true
                })
            });

            // 字段 状态 plan_type
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#plan_type").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#plan_type",
                    name: "plan_type",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"1","name":"一键推文"},{"value":"2","name":"漫画推文"},{"value":"3","name":"爆款二创"},{"value":"4","name":"海外推文"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true
                })
            });

            // 字段 状态 is_example
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#is_example").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#is_example",
                    name: "is_example",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"1","name":"是"},{"value":"0","name":"否"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true
                })
            });

            // 字段 状态 status
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#isdel").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#isdel",
                    name: "isdel",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"1","name":"是"},{"value":"0","name":"否"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true
                })
            });

            // 字段 创作内容 chapterid
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/novel/chapter/select?format=select&limit=1000",
                    dataType: "json",
                    async:true,
                    headers: get_header({"format":"select"}),
                    success: function (res) {
                        layui.xmSelect.render({
                            el: "#chapterid",
                            name: "chapterid",
                            filterable: true,
                            data: res.data,
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                        })
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            // 字段 创作内容 novel_sn
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/novel/index/select?format=select&limit=1000",
                    dataType: "json",
                    async:true,
                    headers: get_header({"format":"select"}),
                    success: function (res) {
                        layui.xmSelect.render({
                            el: "#novel_sn",
                            name: "novel_sn",
                            filterable: true,
                            data: res.data,
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                        })
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
					{
						title: "ID",align: "center", field: "id",width:100,sort:true,
					},{
						title: "用户编号",align: "center", field: "user_sn",width:120,
					},{
						title: "小说编号",align: "center", field: "novel_sn",width:120,
					},{
						title: "小说标题",align: "center", field: "title",width:120,
					},{
						title: "方案名称",align: "center", field: "name",width:120,
					},{
						title: "创作内容",align: "center", field: "content",width:150, templet: function (d) {
                            // 预览数据
                            if(d.plan_type==1){
                                return '<a href="javascript:view('+d.chapterid+');">预览</a> '+d.content;
                            }else if(d.plan_type==2){
                                return '<a href="javascript:article_view('+d.id+');">预览</a> '+d.content;
                            }else if(d.plan_type==3){
                                return '<a href="javascript:video_view('+d.id+');">预览</a> '+d.content;
                            }else if(d.plan_type==4){
                                return '<a href="javascript:view('+d.chapterid+');">预览</a> '+d.content;
                            }

                        }
					},{
                        title: "推文类型",align: "center", field: "status",width:100, templet: function (d) {
                            if(d.plan_type==1){
                                return '<span style="color: #c50ecf">一键推文</span>';
                            }else if(d.plan_type==2){
                                return '<span style="color: #13b162">漫画推文</span>';
                            }else if(d.plan_type==3){
                                return  '<span style="color: #234be3">爆款二创</span>';
                            }else if(d.plan_type==4){
                                return  '<span style="color: #8945d1">海外推文</span>';
                            }
                        }
                    },{
                        title: "状态",align: "center", field: "status",width:120, templet: function (d) {
                            if(d.status==0){
                                return '<span style="color: #c50ecf">待入列</span>';
                            }else if(d.status==1){
                                return '<span style="color: #13b162">生成中</span>';
                            }else if(d.status==2){
                                return  '<span style="color: #234be3">已完成</span>';
                            }else if(d.status==3){
                                return  '<span style="color: #e30b43">生成失败' +
                                    '<i class="layui-icon" id="fail_id'+d.id+'" onmouseover="tips(\''+compile(d.message)+'\','+d.id+')">&#xe60b;</i></span>';
                            }
                        }
                    },{
                        title: "优秀案例",align: "center", field: "is_example",width:90, templet: function (d) {
                            if(d.is_example==1){
                                return '<span style="color: #234be3">是</span>';
                            }else{
                                return '<span style="color: #666666">否</span>';
                            }
                        }
                    },{
                        title: "封面图",align: "center", field: "example_main_image",width:80, templet: function (d) {
                            if(d.example_main_image){
                                return '<img src="'+encodeURI(d['example_main_image'])+'" style="max-width:32px;max-height:32px;" alt="" />'
                            }else{
                                return '';
                            }
                        }
                    },{
						title: "花费时长(秒)",align: "center", field: "finish_tts",width:100,
					},{
						title: "模型风格",align: "center", field: "modelname",width:100,
					},{
						title: "主播音色",align: "center", field: "timbrename",width:100,
					},{
						title: "主播音量",align: "center", field: "timbre_volume",width:100,
					},{
						title: "主播语速",align: "center", field: "speak_speed",width:100,
					},{
						title: "背景音乐",align: "center", field: "musicname",width:100,
					},{
						title: "背景音量",align: "center", field: "music_volume",width:100,
					},{
						title: "播放音乐",align: "center", field: "music_url",width:150,templet: function (d) {
                            if(d['music_url']){
                                return '<audio controls>'+
                                    '<source src="'+d['music_url']+'" type="audio/mpeg">'+
                                    '<object data="'+d['music_url']+'">'+
                                    '</object> '+
                                    '</audio>';
                            }else{
                                return ' ';
                            }
                        }
					},{
						title: "字体样式",align: "center", field: "fontname",width:100,
					},{
						title: "视频比例",align: "center", field: "video_rate",width:100,
					},{
						title: "完成时间",align: "center", field: "finish_time",width:160,
					},{
						title: "执行时间",align: "center", field: "queue_time",width:160,
					},{
						title: "删除",align: "center", field: "isdel",width:100, templet: function (d) {
                            if(d.isdel){
                                return  '<span style="color: #e7294a">已删除</span>';
                            }else{
                                return  '<span style="color: #234be3">正常</span>';
                            }
						}
					}
                    ,{
                        title: '视频',align: "center", field: 'video_url',width:100, templet:function (d) {
                            if(d.video_url){
                                return '<div data-video="'+d.video_url+'" onclick="open_video(this)">' +
                                    '<video width="80" height="40">'+
                                    '<source src="'+d.video_url+'" type="video/mp4">'+
                                    '<object data="'+d.video_url+'" width="500" height="800">'+
                                    '</object> '+
                                    '</video>' +
                                    '</div>';
                            }else{
                                return ' ';
                            }
                        }
                    }
                    , {field: 'created_at', title: '添加时间',width:160,sort:true}
					,{
						title: "操作", toolbar: "#table-bar", align: "center", fixed: "right", width: 170,
					}
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
                    where: where,
                    headers: get_header(where),
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				    done: function () {
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				    }
				});
				
				// 获取表格中下拉或树形组件数据
				let apiResults = {};
				apiResults["position"] = [];
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    } else if (obj.event === "part") {
                        part(obj);
                    } else if (obj.event === "example") {
                        example(obj);
                    } else if (obj.event === "cancel_example") {
                        cancel_example(obj);
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    where = searchWhere(data.field);
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: where,
                        headers: get_header(where)
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    where = {};
                    table.reload("data-table", {
                        where: where,
                        headers: get_header(where)
                    })
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    where.field=obj.field;
                    where.order=obj.type;
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: where,
                        headers: get_header(where)
                    });
                });

                // 表格新增数据
                let add = function() {
                    layer.open({
                        type: 2,
                        title: "新增",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"600px", common.isModile()?"100%":"550px"],
                        content: INSERT_URL
                    });
                }

                // 内容分段
                let part = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "视频分段",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"95%", common.isModile()?"100%":"95%"],
                        content: PART_URL + "?" + "user_planid=" + value
                    });
                }

                // 设为案例
                let example = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "设为案例",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"550px", common.isModile()?"100%":"350px"],
                        content: EXAMPLE_URL + "?" + "user_planid=" + value
                    });
                }

                // 去掉案例
                let cancel_example = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    let data = {};
                    data[PRIMARY_KEY] = value;
                    data['is_example'] = 0;
                    data['example_main_image'] = '';
                    layer.confirm("确定取消案例?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: EXAMPLE_URL,
                            data: data,
                            dataType: "json",
                            type: "post",
                            async:true,
                            headers: get_header(data),
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "修改",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"600px", common.isModile()?"100%":"550px"],
                        content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            async:true,
                            headers: get_header(data),
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }

                window.view = function (chapterid) {
                    layer.open({
                        type: 2,
                        title: "预览",
                        shade: 0.1,
                        maxmin: true,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"750px", common.isModile()?"100%":"620px"],
                        content: VIEW_URL + "?" + "id=" + chapterid
                    });
                }

                window.article_view = function (id) {
                    layer.open({
                        type: 2,
                        title: "漫画推文",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"95%", common.isModile()?"100%":"80%"],
                        content: ARTICLE_VIEW_URL + "?" + "user_planid=" + id
                    });
                }

                window.video_view = function (id) {
                    layer.open({
                        type: 2,
                        title: "爆款二创",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"95%", common.isModile()?"100%":"80%"],
                        content: VIDEO_VIEW_URL + "?" + "user_planid=" + id
                    });
                }
            })
            function tips(message,number) {
                layer.tips(uncompile(message), '#fail_id'+number);
            }
        </script>
    </body>
</html>
